package com.cyy.daijia.driver.service.impl;

import com.cyy.daijia.common.constant.RedisConstant;
import com.cyy.daijia.common.execption.CException;
import com.cyy.daijia.common.result.Result;
import com.cyy.daijia.common.result.ResultCodeEnum;
import com.cyy.daijia.dispatch.client.NewOrderFeignClient;
import com.cyy.daijia.driver.client.DriverInfoFeignClient;
import com.cyy.daijia.driver.service.DriverService;
import com.cyy.daijia.map.client.LocationFeignClient;
import com.cyy.daijia.model.form.driver.DriverFaceModelForm;
import com.cyy.daijia.model.form.driver.UpdateDriverAuthInfoForm;
import com.cyy.daijia.model.vo.driver.DriverAuthInfoVo;
import com.cyy.daijia.model.vo.driver.DriverLoginVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
@RequiredArgsConstructor
public class DriverServiceImpl implements DriverService {
    private final NewOrderFeignClient  newOrderFeignClient;
    private final LocationFeignClient locationFeignClient;
    private final RedisTemplate redisTemplate;
    private final  DriverInfoFeignClient driverInfoFeignClient;
    @Override
    public String login(String code) {
        Result<Long> login = driverInfoFeignClient.login(code);
        if(login.getCode()!=200){
            throw new CException(ResultCodeEnum.DATA_ERROR);
        }
        if(login.getData()==null){
            throw new CException(ResultCodeEnum.DATA_ERROR);
        }
        String token= UUID.randomUUID().toString().replaceAll("-","");
        redisTemplate.opsForValue().set(RedisConstant.USER_LOGIN_KEY_PREFIX+token,login.getData().toString(),60*60, TimeUnit.SECONDS);
        return token;
        //RedisConstant.USER_LOGIN_KEY_PREFIX
    }

    @Override
    public DriverLoginVo getDriverLoginInfo(Long driverId) {
        return driverInfoFeignClient.getDriverLoginInfo(driverId).getData();
    }

    @Override
    public Boolean updateDriverAuthInfo(UpdateDriverAuthInfoForm updateDriverAuthInfoForm) {
        return driverInfoFeignClient.UpdateDriverAuthInfo(updateDriverAuthInfoForm).getData();
    }

    @Override
    public DriverAuthInfoVo getDriverAuthInfo(Long driverId) {
        return driverInfoFeignClient.getDriverAuthInfo(driverId).getData();
    }

    @Override
    public Boolean creatDriverFaceModel(DriverFaceModelForm driverFaceModelForm) {
        return driverInfoFeignClient.creatDriverFaceModel(driverFaceModelForm).getData();
    }

    @Override
    public Boolean isFaceRecognition(Long driverId) {
        return driverInfoFeignClient.isFaceRecognition(driverId).getData();
    }

    @Override
    public Boolean verifyDriverFace(DriverFaceModelForm driverFaceModelForm) {
        return driverInfoFeignClient.verifyDriverFace(driverFaceModelForm).getData();
    }

    @Override
    public Boolean startService(Long driverId) {
        //检查是否登录 --ok
        //完成人脸识别
        Integer authStatus = driverInfoFeignClient.getDriverLoginInfo(driverId).getData().getAuthStatus();
        if(authStatus!=2){
            System.err.println(1);
            throw new CException(ResultCodeEnum.AUTH_ERROR);
            //认证未通过
        }
        //判断是否当天完成人脸识别
        if(!driverInfoFeignClient.isFaceRecognition(driverId).getData()){
            System.err.println(2);
            throw new CException(ResultCodeEnum.FACE_ERROR);
        }
        //更新司机状态
        System.err.println(3);
        driverInfoFeignClient.updateServiceStatus(driverId,1);
        //删除redis位置信息 map服务
        System.err.println(4);
        locationFeignClient.removeDriverLocation(driverId);
        //清空司机临时redislist队列里面的信息，定时服务
        System.err.println(5);
        newOrderFeignClient.clearNewOrderQueueData(driverId);
        return true;
    }

    @Override
    public Boolean stopService(Long driverId) {
        //更新司机状态
        driverInfoFeignClient.updateServiceStatus(driverId,0);
        //删除redis中位置信息
        //删除redis位置信息 map服务
        locationFeignClient.removeDriverLocation(driverId);
        //清空司机临时redislist队列里面的信息，定时服务
        newOrderFeignClient.clearNewOrderQueueData(driverId);
        return true;
    }
}
